﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace WPFCollection
{
    [System.Security.Permissions.PermissionSet(System.Security.Permissions.SecurityAction.Demand, Name = "FullTrust")]
    [System.Runtime.InteropServices.ComVisibleAttribute(true)]
    public class WB
    {
        private MainWindow _Mw = null;
        /// <summary>
        /// 指定窗口
        /// </summary>
        internal MainWindow Mw
        {
            get { return _Mw; }
            set { _Mw = value; }
        }

        string elContent = "";
        /// <summary>
        /// 当前选择原素的内容
        /// </summary>
        internal string ElContent
        {
            get { return elContent; }
        }

        /// <summary>
        /// 当前选择原素的上一级原素的内容
        /// </summary>
        private string _ParentElContent = "";
        /// <summary>
        /// 当前选择原素的上一级原素的内容
        /// </summary>
        internal string ParentElContent
        {
            get { return _ParentElContent; }
        }

        /// <summary>
        /// 正则表达式集合
        /// </summary>
        System.Collections.ObjectModel.ObservableCollection<string> regs = null;


        /// <summary>
        /// 选中原素
        /// </summary>
        public void SelectElement(string e)
        {
            Mw.ttHTMLContent.Text = e;

            if (elContent != "")
            {
                CreateReg(e.Split(' ')[0], elContent);

                Filter();
            }
        }
        /// <summary>
        /// 设置选择原素的内容
        /// </summary>
        /// <param name="content">内容</param>
        public void SetElContent(string content)
        {
            elContent = content;
        }
        /// <summary>
        /// 当前选择原素的上一级原素的内容
        /// </summary>
        /// <param name="content">内容</param>
        public void SetParentElContent(string content)
        {
            _ParentElContent = content;
        }



        #region 生成正则表达式
        /// <summary>
        /// 跟据选择原素的路径生成正则表达式
        /// </summary>
        /// <param name="SelectElementPath">选择原素的路径</param>
        /// <param name="elContent">原素中的内容</param>
        private void CreateReg(string SelectElementPath, string elContent)
        {
            

            //得到单个路径 HTML-body
            System.Collections.Generic.IEnumerable<string> paths = SelectElementPath.Split(new string[] { "-" }, StringSplitOptions.RemoveEmptyEntries);

            regs = new System.Collections.ObjectModel.ObservableCollection<string>();


            //开始生成表达式
            for (int k = 0; k < paths.Count(); k++)
            {

                var item = paths.ElementAt(k);
                if (item == "")
                {
                    continue;
                }

                regs.Add("(<" + item.ToUpper() + "|<" + item.ToLower() + "|<" + item + ")"
                    + @"[\W\w]+"
                    +
                    "(</" + item.ToUpper() + ">|</" + item.ToLower() + ">|</" + item + ">)"
                    );

            }


        }
        #endregion

        #region filter 过滤内容
        private void Filter()
        {
           
            System.Net.WebClient wb = new System.Net.WebClient();
            String content =  wb.DownloadString(Mw.wb.Source);
            string tempContent = content;
            //开始表达式过滤
            //得到最后一层的数据
           System.Text.RegularExpressions.MatchCollection matchCollection  =  System.Text.RegularExpressions.Regex.Matches(content, regs[regs.Count-1]);

            for (int k = 0; k < regs.Count; k++)
            {
               //不区分大小写
               tempContent =   System.Text.RegularExpressions.Regex.Matches(tempContent,regs[k], System.Text.RegularExpressions.RegexOptions.IgnoreCase
                   )
                    
                   .Cast<System.Text.RegularExpressions.Match>()
                   .FirstOrDefault(p=>p.Value.Contains(ElContent)).Value
                   ;
           }


            //如果得到的内容等于1 就直接返回数据
            if (matchCollection.Count == 1)
           {
               //得到当前原素的上一级原素
               System.Xml.Linq.XDocument Xdom = System.Xml.Linq.XDocument.Parse(_ParentElContent);
           }

        }
        #endregion

    }
}
